<?php

/**
 * Website Controller
 * @author Administrator
 */
class WebsiteController {
    // TODO - Insert your code here
    private $path = "";
    private $lan;

    /**
     */
    function __construct() {

        $this->path = "../src/module/admin/page_webinfo/temp/";

        if (isset( $_SESSION[SESSION_LANGUAGE])) {
        	$this->lan = $_SESSION[SESSION_LANGUAGE];
        } else {
        	$this->lan = DEFAULT_LANGAUGE;
        }
    }

    /**
     * view form function
     */
    public function viewForm() {
        
        // create xTemplate
        $template = new XTemplate( $this->path . $this->lan . "/form.html");
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_PAGE_WEBINFO);
        $template->assign( ACTION, PVALUE_ACTION_SUBMIT_FORM);
        
        $db = new DBAccess();
        $website = $db->selectOneByCause($GLOBALS['webinfoFullColArray'], WEBINFO, array(WEBINFO_LANGUAGE => $this->lan));
        
        if ($website == null || count($website) ==0){
            $website = array();
            $website[WEBINFO_ID] = 0;
            $website[WEBINFO_TITLETAG] = "";
            $website[WEBINFO_ICON] = "";
            $website[WEBINFO_METAKEYWORD] = "";
            $website[WEBINFO_METADESCRIPTION] = "";
            $website[WEBINFO_HEADERSCRIPT] = "";
            $website[WEBINFO_BODYSCRIPT] = "";
            $website[WEBINFO_BACKLINK] = "";
            $website[WEBINFO_LANGUAGE] = "";
            $website[WEBINFO_EDITBY] = "";
        }
        
        // assign form item
        $template->assign( FORMITEM, $website);
        $template->parse( PAGECONTENT .".".FORMITEM);
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }

    /**
     * submit form function
     * @return multitype:string
     */
    public function submitForm() {
        
        // reponse array
        $response_array = array();
        
        // get input
        $website[WEBINFO_ID] = $_REQUEST[WEBINFO_ID];
        $website[WEBINFO_TITLETAG] = $_REQUEST[WEBINFO_TITLETAG];
        $website[WEBINFO_ICON] = $_REQUEST[WEBINFO_ICON];
        $website[WEBINFO_METAKEYWORD] = $_REQUEST[WEBINFO_METAKEYWORD];
        $website[WEBINFO_METADESCRIPTION] = $_REQUEST[WEBINFO_METADESCRIPTION];
        $website[WEBINFO_HEADERSCRIPT] = $_REQUEST[WEBINFO_HEADERSCRIPT];
        $website[WEBINFO_BODYSCRIPT] = $_REQUEST[WEBINFO_BODYSCRIPT];
        $website[WEBINFO_BACKLINK] = $_REQUEST[WEBINFO_BACKLINK];
        $website[WEBINFO_LANGUAGE] = $this->lan;
        $website[WEBINFO_EDITBY] = $_SESSION[SESSION_USERNAME];
        
        
        // update data to database
        $db = new DBAccess();
        
        if ($website[WEBINFO_ID] == 0) {
			if ($db->insertData( $website, WEBINFO)) {
				$response_array[STATUS] = STATUS_OK;
				$response_array[MESSAGE] = getMessage(MESSAGE_INSERT_SUCCESSFUL);
				$response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_PAGE_WEBINFO . "&act=" . PVALUE_ACTION_VIEW_FORM;
			} else {
				$response_array[STATUS] = STATUS_FAIL;
				$response_array[MESSAGE] = getMessage(MESSAGE_INSERT_FAIL);
			}
		} else {
			if ($db->updateData( $website, WEBINFO)) {
				$response_array[STATUS] = STATUS_OK;
				$response_array[MESSAGE] = getMessage(MESSAGE_UPDATE_SUCCESSFUL);
				$response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_PAGE_WEBINFO . "&act=" . PVALUE_ACTION_VIEW_FORM;
			} else {
				$response_array[STATUS] = STATUS_FAIL;
				$response_array[MESSAGE] = getMessage(MESSAGE_UPDATE_FAIL);
			}
		}
		
		// sleep(4);
		return $response_array;
        

    }

    /**
     */
    function __destruct() {
        
        // TODO - Insert your code here
    }
}

?>